//
//  SaleController.m
//  SmallEasyLife
//
//  Created by qianfeng on 15/10/23.
//  Copyright © 2015年 GXK. All rights reserved.
//

#import "SaleController.h"
#import "SaleTitleView.h"
#import "SaleContentView.h"
#import "SaleDataManager.h"

#import "Themes.h"

#import "SaleEventController.h"

#import "BDNodeModel.h"
#import "Themes.h"

@interface SaleController ()<SaleContentCellDelegate>

@property (weak, nonatomic) IBOutlet SaleTitleView *saleTitleView;
@property (weak, nonatomic) IBOutlet SaleContentView *contentView;

@property (nonatomic, strong) NSMutableArray * BADataArray;
@property (nonatomic, strong) NSArray * BDDataArray;

@end

@implementation SaleController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    [self loadDataFromCache];
}
- (void)loadDataFromCache
{
    if (self.BADataArray == nil) {
        self.BADataArray = [[NSMutableArray alloc] init];
    }
    
    
    NSString * path1 = [[NSBundle mainBundle] pathForResource:@"SalePage1" ofType:@"json"];
    NSData * data1 = [NSData dataWithContentsOfFile:path1];
    
    NSDictionary * dic1 = [NSJSONSerialization JSONObjectWithData:data1 options:NSJSONReadingMutableContainers error:nil];
//    NSLog(@"%@",dic);
//    NSNumber * code = dic[@"code"];
//    NSString * msg = dic[@"msg"];
    
//    NSLog(@"code - > %@",code);
//    NSLog(@"msg - > %@", msg);
    
    NSArray * modelArr1 = dic1[@"list"];
    self.BDDataArray = [SaleDataManager analysisBDNodes:modelArr1];
//    NSLog(@"self.bddataArray -> %@",self.BDDataArray);
    
//    NSLog(@"%@",modelArr);
    
//    themesModel可以读出数据
    BDNodeModel * nodeModel = [self.BDDataArray firstObject];
    Themes * themesModel = [nodeModel.themes firstObject];
//    NSLog(@"themesModel in vc -> %@",themesModel);
    
    
    NSString * path2 = [[NSBundle mainBundle] pathForResource:@"SalePage2" ofType:@"json"];
    NSData * data2 = [NSData dataWithContentsOfFile:path2];
    
    NSDictionary * dic2 = [NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];
    
    NSArray * modelArr2 = dic2[@"list"];
//    NSLog(@"%@",modelArr2);
    for (NSDictionary * modelDic2 in modelArr2) {
        Themes * model = [Themes themesWithDictionary:modelDic2];
        [self.BADataArray addObject:model];
    }
    
    self.contentView.leftDataArray = self.BDDataArray;
    self.contentView.rightDataArray = self.BADataArray;
    self.contentView.delegate = self;
    self.contentView.ownerViewController = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    SaleContentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SaleContentCell" forIndexPath:indexPath];
//    
//    // Configure the cell...
//    cell.delegate = self;
//    //左边视图（美天）的数据
//    cell.leftDataArray = self.BDDataArray;
//    //右边视图（美辑）的数据
//    cell.rightDataArray = self.BADataArray;
//    
//    cell.ownerViewController = self.navigationController;
//    
//    return cell;
//}

- (void)saleContentCellScrollToPage:(NSInteger)page
{
    self.saleTitleView.currentPage = page;
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    
    if ([segue.identifier isEqualToString:@"SaleEventSegue"]) {
        if ([segue.destinationViewController isKindOfClass:[SaleEventController class]]) {
            SaleEventController * eventController = segue.destinationViewController;
            eventController.model = sender;
        }
    }
}


@end
